﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GLImp;
using System.Drawing;
using CastleOfTheDisillusioned.Location;

namespace CastleOfTheDisillusioned.Enemies {
	class Wolf : Enemy {
		enum State {
			Idle, Moving, Attacking
		}

		State CurrentState = State.Idle;
		double WalkSpeed = 0.2;
		double AttackSpeed = 0.8;
		double HSpeed = 0;

		Sprite WolfIdle = new Sprite();
		Sprite WolfRunning = new Sprite();
		Sprite WolfAttacking = new Sprite();
		
		public Wolf(double X, double Y) {
			this.X = X;
			this.Y = Y;

			this.Width = 2;
			this.Height = 1;
			this.Health = 2;

			Facing = Direction.Left;

			TileSheet ts = new TileSheet(Textures.Wolf, 75, 50);

			//Idle
			for (int i = 0; i < 4; i++) {
				WolfIdle.Add(ts.Get(i, 0));
			}
			WolfIdle.PlaybackSpeed = 0.1;
			WolfIdle.XOrigin = 0.5;
			WolfIdle.YOrigin = 0.9;

			//Running
			for (int i = 0; i < 6; i++) {
				WolfRunning.Add(ts.Get(i, 2));
			}
			WolfRunning.PlaybackSpeed = 0.2;
			WolfRunning.XOrigin = 0.5;
			WolfRunning.YOrigin = 0.9;

			//Attacking
			for (int i = 0; i < 6; i++) {
				WolfAttacking.Add(ts.Get(i, 1));
			}
			WolfAttacking.PlaybackSpeed = 0.2;
			WolfAttacking.XOrigin = 0.5;
			WolfAttacking.YOrigin = 0.9;
		}

		public override void Draw() {
			//GraphicsManager.DrawRectangle(X, Y, Width, Height, Color.Gray);

			Sprite CurrentSprite = WolfIdle;
			switch (CurrentState) {
				case State.Idle:
					CurrentSprite = WolfIdle;
					break;
				case State.Moving:
					CurrentSprite = WolfRunning;
					break;
				case State.Attacking:
					CurrentSprite = WolfAttacking;
					break;
			}


			if (Facing == Direction.Left) {
				CurrentSprite.Draw(X, Y, 3, 2);
			} else {
				CurrentSprite.Draw(X + 3, Y, -3, 2);
			}
		}

		public override void Update() {
			base.Update();

			Player p = Location_State.CurrentPlayer;
			double DiffX = Math.Abs(p.X - X);

			switch (CurrentState){
				case State.Idle:
					//If too far away, just go into idle state
					if (DiffX < 10) {
						CurrentState = State.Moving;
					}
					TouchDamage = false;
					break;

				case State.Moving:
					int Dir = Math.Sign(p.X - this.X);
					Move(Dir * WalkSpeed, 0);
					Facing = (Dir < 0) ? Direction.Left : Direction.Right;

					//If we are close, attack!
					if (DiffX < 3) {
						CurrentState = State.Attacking;
						HSpeed = AttackSpeed;
					}
					TouchDamage = false;
					break;

				case State.Attacking:
					Dir = Facing == Direction.Left ? -1 : 1;
					Move(Dir * HSpeed, 0);
					HSpeed /= 1.2;
					if (HSpeed < 0.005) {
						CurrentState = State.Idle;
					}
					TouchDamage = true;
					break;
			}
		}
	}
}
